package cmd

import (
	"concurrent_runner/runner"
	"context"
	"fmt"
	"github.com/spf13/cobra"
)

var runnerCmd = &cobra.Command{
	Use:   "run",
	Short: "执行",
	Long:  "执行命令",
	Run: func(cmd *cobra.Command, args []string) {
		ctx, cancel := context.WithCancel(context.Background())
		r := runner.InitRunner(maxProcess, concurrentNumber, command, batchCommandFile, ctx)
		if batchCommandFile != "" {
			fmt.Println("执行文件:", batchCommandFile)
			r.StartFile()
		} else if command != "" {
			r.StartCommand()
			fmt.Println("执行命令:", concurrentNumber, command)
		} else {
			fmt.Println("没有命令执行")
		}
		r.Wait()
		cancel()
	},
}

func init() {
	rootCmd.AddCommand(runnerCmd)
}
